package com.aoxon.cargo.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

/**
 * 自定义Scrollview控件
 * 
 * @author ZZJ
 * 
 */
public class MyScrollView extends ScrollView {

	public View view;

	public MyScrollView(Context context) {
		super(context);
	}
	public MyScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context,attrs,defStyle);
	}

	
	
	public int computerVreticalScrollRange() {
		return super.computeHorizontalScrollRange();
	}

	public int computerVerticalScrollOffset() {
		return super.computeHorizontalScrollOffset();
	}

	public void getView(){
		this.view = getChildAt(0);
		if(view != null){
			this.setOnTouchListener(myTouchListener);
		}
	}
	
	Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				if (onScrollListener != null) {
					if (view.getMeasuredHeight() <= getScrollY() + getHeight()) {
						onScrollListener.onBottom();
					} else {
						if (getScrollY() == 0) {
							onScrollListener.onTop();
						} else {
							if(view.getMeasuredHeight() * 2 / 3 <= getScrollY() + getHeight()) {
								onScrollListener.onWillToBottom();
							}
							onScrollListener.onScroll();
						}
					}
				}
				break;

			default:
				break;
			}
		}

	};

	OnTouchListener myTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				break;
			case MotionEvent.ACTION_UP:
				if (view != null && onScrollListener != null) {
					myHandler.sendMessageDelayed(myHandler.obtainMessage(1),
							200);
				}
			default:
				break;
			}
			return false;
		}
	};

	/**
	 * 定义接口
	 * 
	 */
	public interface OnScrollListener {
		void onBottom();

		void onTop();

		void onScroll();
		
		// 即将到达底部（2/3处）
		void onWillToBottom();
	}

	private OnScrollListener onScrollListener;

	public void setOnScrollListener(OnScrollListener onScrollListener) {
		this.onScrollListener = onScrollListener;
	}

}
